Add Sign to all files


In [16]:
import os
import sys
import shutil

In [17]:
def rename_all_files(CWD,sign):
    #CWD -current working dir
    #rename all files
    #if dir then go in & rename all sub-files
    files=list(os.listdir(CWD))
    os.chdir(CWD)
    #start renaming recursively
    for fle in files:
        if not os.path.isdir(fle):
            if fle.find(sign) == -1:
                ls=fle.split(".")
                new_name=".".join(ls[:-1])+"- "+sign+"."+ls[-1]
                os.rename(fle,new_name)
        else:
            rename_all_files(os.path.join(CWD,fle),sign)
    return os.chdir("..")

Enter base directory & sign(sign to add)


In [25]:
print "location format windows E:\\temp \n"
#base_dir="E:\\temp"
base_dir = raw_input('Enter base file location:')
sign='[Codecops.in]'
#sign =raw_input("Enter your sign : ")


location format windows E:\temp 

Enter base file location:E:\movies

In [26]:
#move to base directory
print os.getcwd()
os.chdir(base_dir)
print os.getcwd()


E:\
E:\movies

In [27]:
## __main__
print 'Base address is :',os.getcwd()
check_point=raw_input("R you sure to perform operation[y/n] : ")
if check_point=='y' or check_point =='Y':
    rename_all_files(os.getcwd(),sign)
    print "Done Thank You"
else:
    print "Ok Bye"
    exit()


Base address is : E:\movies
R you sure to perform operation[y/n] : y
Done Thank You

In [21]:
os.getcwd()


Out[21]:
'E:\\DC++\\sem8\\pdy'